home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / c / library / mslang / mfcvbx / bigicon.cp_ / bigicon.cp
Encoding:
Text File  |  1993-10-01  |  9.9 KB  |  443 lines

  1. //
  2. // $Id$
  3. //
  4. // Program    :     BigIcon.cpp
  5. //
  6. // Author    :     James Bish
  7. //
  8. // Date        :     06/02/93
  9. //
  10. // Purpose    :     To create a BigIcon custom control (vbx) for Visual
  11. //                Basic.
  12. // $Log$
  13. //
  14. // Copyright (c) Bish Programming 1993 - All Rights Reserved.
  15. //
  16.  
  17. #include "stdafx.h"
  18.  
  19. //
  20. // Methods for the Popup Window.
  21. //
  22.  
  23. myPopupWnd::myPopupWnd(CVBCtlWnd *pCtl, USHORT iProperty) 
  24.     : CVBPopupWnd(pCtl, iProperty) 
  25. {
  26. }
  27.  
  28. // 
  29. // Create the control window.
  30. //
  31. IMPLEMENT_DYNCREATE(myCtlWnd, CVBCtlWnd)
  32.                                
  33. myCtlWnd::myCtlWnd()
  34. {
  35. }
  36.                               
  37. myCtlWnd::myCtlWnd(HWND hwnd)
  38. {
  39.     CVBCtlWnd::CVBCtlWnd(hwnd);
  40. }
  41.  
  42.  
  43. //
  44. // The CVBDLL derived application object.
  45. //
  46. myVBDLL theApp;          
  47.  
  48. //
  49. // myVBDLL::InitInstance()
  50. //
  51. // Perform one time initialization here, eg. (register the visual basic
  52. // control models.
  53. //
  54. BOOL myVBDLL::InitInstance()  
  55. {   
  56.     SetPopupWnd(FALSE); 
  57.     
  58.     //
  59.     // Add the VB Control Model.
  60.     //
  61.     
  62.     AddModel("BigIcon", 0,CS_VREDRAW | CS_HREDRAW, 0, sizeof(BigIcon),
  63.         IDBMP_BIGICON, NULL, IPROP_BIGICON_CTLNAME,
  64.         IEVENT_BIGICON_CLICK, -1, RUNTIME_CLASS(myCtlWnd)); 
  65.  
  66.     //
  67.     // Add the standard properties.
  68.     //
  69.         
  70.     AddProperty("BigIcon", PPROPINFO_STD_CTLNAME );
  71.     AddProperty("BigIcon", PPROPINFO_STD_INDEX );
  72.     AddProperty("BigIcon", PPROPINFO_STD_LEFT );
  73.     AddProperty("BigIcon", PPROPINFO_STD_TOP );
  74.     AddProperty("BigIcon", PPROPINFO_STD_WIDTH );
  75.     AddProperty("BigIcon", PPROPINFO_STD_HEIGHT );
  76.     AddProperty("BigIcon", PPROPINFO_STD_VISIBLE );
  77.     AddProperty("BigIcon", PPROPINFO_STD_PARENT );
  78.     AddProperty("BigIcon", PPROPINFO_STD_DRAGMODE );
  79.     AddProperty("BigIcon", PPROPINFO_STD_DRAGICON );
  80.     AddProperty("BigIcon", PPROPINFO_STD_TAG );
  81.     AddProperty("BigIcon", PPROPINFO_STD_HWND );
  82.     AddProperty("BigIcon", PPROPINFO_STD_BACKCOLOR );
  83.     
  84.     //
  85.     // Add custom properties.
  86.     //
  87.         
  88.     AddProperty("BigIcon", "Picture",
  89.         DT_PICTURE | PF_fGetData | PF_fSetCheck | PF_fSetData |
  90.         PF_fSaveData | PF_fSetMsg, OFFSETIN(BigIcon,hpicPict));
  91.     
  92.     AddProperty("BigIcon", "ShadowWidth", 
  93.         DT_SHORT | PF_fGetData | PF_fSetMsg | PF_fSetData | PF_fSaveData,
  94.         OFFSETIN(BigIcon, ShadowWidth));
  95.     
  96.     AddProperty("BigIcon", "ShadowColor", 
  97.         DT_COLOR | PF_fGetData | PF_fSetMsg | PF_fSetData | PF_fSaveData,
  98.         OFFSETIN(BigIcon, ShadowColor));
  99.         
  100.     AddProperty("BigIcon", "(About)",
  101.         DT_BOOL | PF_fGetData | PF_fSetMsg | PF_fSaveData | PF_fGetHszMsg,
  102.         OFFSETIN(BigIcon, About));
  103.     
  104.     //
  105.     // Add standard events.
  106.     //
  107.                 
  108.     AddEvent("BigIcon", PEVENTINFO_STD_CLICK );
  109.     AddEvent("BigIcon", PEVENTINFO_STD_DRAGDROP );
  110.     AddEvent("BigIcon", PEVENTINFO_STD_DRAGOVER );
  111.        
  112.     return TRUE;
  113.  
  114. //
  115. // Message map for myCtlWnd
  116. //
  117. BEGIN_MESSAGE_MAP(myCtlWnd, CVBCtlWnd)
  118.     ON_WM_PAINT()
  119.     ON_WM_CREATE() 
  120.     ON_WM_NCDESTROY()
  121.         
  122.     ON_MESSAGE(VBM_SETPROPERTY,OnVBSetProperty)
  123.     ON_MESSAGE(VBM_CHECKPROPERTY, OnVBCheckProperty)
  124.     ON_MESSAGE(VBM_GETPROPERTYHSZ, OnVBGetPropertyHsz)
  125.     ON_MESSAGE(VBM_INITPROPPOPUP, OnVBInitPropPopup)
  126. END_MESSAGE_MAP()
  127.  
  128. //
  129. // myCtlWnd::OnPaint()
  130. //
  131. // Handle painting of the control.
  132. //
  133. void myCtlWnd::OnPaint()
  134. {    
  135.     CPaintDC     *pDC = new CPaintDC(this); // device context for painting
  136.     HPIC        hpic;    
  137.     PIC            pic;
  138.     CRect         rect;
  139.     GetClientRect(rect);
  140.                 
  141.     int         cxClient = rect.Width();
  142.     int         cyClient = rect.Height();
  143.     COLORREF     ShadowColor = (COLORREF)LpBigIconDEREF(m_hCtl)->ShadowColor;
  144.     int            ShadowWidth = (int)LpBigIconDEREF(m_hCtl)->ShadowWidth;
  145.  
  146.     //
  147.     // Fill the window with the color of the parent window.
  148.     //
  149.         
  150.     CBrush *cBr;
  151.     
  152.     cBr = CBrush::FromHandle( (HBRUSH) 
  153.         GetParent()->SendMessage(WM_CTLCOLOR,(WPARAM)pDC->m_hDC, 
  154.         MAKELONG(m_hWnd,0)));               
  155.     pDC->FillRect(rect, cBr);
  156.  
  157.     //
  158.     // Draw the border around the window.
  159.     //
  160.         
  161.     CPen pen;
  162.     pen.CreateStockObject(BLACK_PEN);
  163.     CPen* pPenOld = pDC->SelectObject(&pen);
  164.     pDC->Rectangle(0, 0, cxClient-ShadowWidth, cyClient-ShadowWidth);
  165.     if (pPenOld) {
  166.         pDC->SelectObject(pPenOld);
  167.         pen.DeleteObject();
  168.     }            
  169.     
  170.     //
  171.     // draw shadows around icon
  172.     //
  173.     CBrush br;
  174.     br.CreateSolidBrush(ShadowColor);
  175.     rect.SetRect(cxClient-ShadowWidth, ShadowWidth, cxClient, cyClient);
  176.     pDC->FillRect(rect, &br);
  177.     rect.SetRect(ShadowWidth, cyClient-ShadowWidth, cxClient, cyClient);
  178.     pDC->FillRect(rect, &br);
  179.  
  180.     //
  181.     // Get the picture.
  182.     //
  183.     
  184.     hpic = LpBigIconDEREF(m_hCtl)->hpicPict;
  185.     
  186.     //
  187.     // Set the palette.
  188.     //
  189.     
  190.     if( VBGetVersion() >= VB200_VERSION ) 
  191.         VBGetPicEx(hpic, &pic, VB_VERSION );
  192.     else {
  193.         pic.picData.bmp.hpal = NULL;
  194.         VBGetPic(hpic, &pic );
  195.     }
  196.     
  197.     //
  198.     // Draw the picture.
  199.     //
  200.     
  201.     switch( pic.picType ) {    
  202.         case PICTYPE_BITMAP: {
  203.                 BITMAP bitmap;                
  204.                 GetObject(pic.picData.bmp.hbitmap, sizeof(BITMAP),
  205.                     (LPSTR)&bitmap);
  206.                     
  207.                 CDC dcMem;
  208.                 if( !dcMem.CreateCompatibleDC(pDC))
  209.                     break;
  210.                     
  211.                 dcMem.SelectObject(pic.picData.bmp.hbitmap);
  212.  
  213.                 pDC->StretchBlt(2, 2, cxClient-ShadowWidth-ShadowWidth, 
  214.                     cyClient-ShadowWidth-ShadowWidth,
  215.                     &dcMem, 0, 0, bitmap.bmWidth, bitmap.bmHeight, SRCCOPY);                
  216.             }    
  217.             break;
  218.             
  219.         case PICTYPE_ICON: {
  220.                 // draw icon into off-screen bitmap
  221.                 int cxIcon = ::GetSystemMetrics(SM_CXICON);
  222.                 int cyIcon = ::GetSystemMetrics(SM_CYICON);
  223.             
  224.                 CBitmap bitmap;
  225.                 if( !bitmap.CreateCompatibleBitmap(pDC,cxIcon,cyIcon))
  226.                     break;
  227.                 
  228.                 CDC dcMem;
  229.                 if( !dcMem.CreateCompatibleDC(pDC))
  230.                     break;
  231.                 
  232.                 dcMem.SelectObject(&bitmap);
  233.             
  234.                 dcMem.StretchBlt(0,0, cxIcon, cyIcon, pDC,
  235.                     2, 2, cxClient-ShadowWidth-ShadowWidth, 
  236.                     cyClient-ShadowWidth-ShadowWidth, 
  237.                     SRCCOPY);
  238.                 
  239.                 dcMem.DrawIcon(0,0,pic.picData.icon.hicon);                                                                
  240.         
  241.                 pDC->StretchBlt(2, 2, cxClient-ShadowWidth-ShadowWidth, 
  242.                     cyClient-ShadowWidth-ShadowWidth,
  243.                     &dcMem, 0, 0, cxIcon, cyIcon, SRCCOPY);
  244.             }
  245.             break;    
  246.  
  247.         case PICTYPE_METAFILE: {
  248.                 pDC->SetMapMode(MM_ANISOTROPIC);
  249.                 pDC->SetWindowExt(1000,1000);
  250.                 pDC->SetWindowOrg(-2,-2);
  251.                 pDC->SetViewportExt(cxClient-ShadowWidth,cyClient-ShadowWidth);        
  252.                 pDC->PlayMetaFile((HMETAFILE) pic.picData.wmf.hmeta);   
  253.             }                    
  254.             break;
  255.                         
  256.         case PICTYPE_NONE:
  257.             break;
  258.     }
  259.     
  260.     delete pDC;
  261. }
  262.  
  263. //
  264. // myCtlWnd::OnVBSetProperty
  265. //
  266. // Handle changes in the user properties of the control
  267. //
  268. LRESULT myCtlWnd::OnVBSetProperty(WPARAM wp, LPARAM lp)
  269. {
  270.     switch( wp ) {
  271.         case IPROP_BIGICON_BKCOLOR: 
  272.             InvalidateRect(NULL, TRUE );
  273.             return( 0L );
  274.             
  275.         case IPROP_BIGICON_SHADOWCOLOR:
  276.             LpBigIconDEREF(m_hCtl)->ShadowColor = (COLORREF)lp;
  277.             InvalidateRect( NULL, TRUE );
  278.             return( 0L );
  279.             
  280.         case IPROP_BIGICON_SHADOWWIDTH:
  281.             LpBigIconDEREF(m_hCtl)->ShadowWidth = (SHORT) lp;
  282.             InvalidateRect( NULL, TRUE );
  283.             return( 0L );
  284.     }
  285.  
  286.     //
  287.     // Return the parent function.
  288.     //
  289.     
  290.     return CVBCtlWnd::OnVBSetProperty(wp, lp);
  291. }
  292.  
  293. //
  294. // myCtlWnd::OnVBCheckProperty
  295. //
  296. // Force a repaint of the control if any of the property values
  297. // changes
  298. //
  299. LRESULT myCtlWnd::OnVBCheckProperty(WPARAM wp, LPARAM lp )
  300. {                 
  301.     switch( wp ) {
  302.         case IPROP_BIGICON_PICT:
  303.             InvalidateRect( NULL, TRUE);
  304.             return( 0L );
  305.     }
  306.  
  307.     //
  308.     // Call the parent method.
  309.     //
  310.              
  311.     return CVBCtlWnd::OnVBCheckProperty(wp, lp);         
  312. }                
  313.  
  314. //
  315. // myCtlWnd::OnVBGetPropertyHsz
  316. //
  317. // Force the string for the About Box message.
  318. //
  319. LRESULT myCtlWnd::OnVBGetPropertyHsz(WPARAM wp, LPARAM lp)
  320. {
  321.     switch( wp ) {
  322.          case IPROP_BIGICON_ABOUT:    
  323.              HSZ        the_hsz;
  324.              
  325.              the_hsz = ::VBCreateHsz((HANDLE) _FP_SEG(m_hCtl), 
  326.                  "Click on \"...\" for About Box");
  327.             _fmemcpy( (LPSTR *)lp, (LPSTR *) &the_hsz, sizeof(HSZ));
  328.             return( 0L );            
  329.         break;
  330.     }
  331.                 
  332.     //
  333.     // Call the parent method.
  334.     //
  335.     
  336.     return CVBCtlWnd::OnVBGetPropertyHsz(wp, lp);
  337. }                
  338.  
  339. //
  340. // myCtlWnd::OnVBInitPropPopup
  341. //
  342. // Create a window and pass a handle back to visual basic.  This
  343. // must be done to display dialog boxes in Visual Basic.
  344. //
  345. LRESULT myCtlWnd::OnVBInitPropPopup(WPARAM wp, LPARAM lp )
  346. {                 
  347.     switch( wp ) {
  348.         case IPROP_BIGICON_ABOUT: {
  349.             myPopupWnd *x = new myPopupWnd(this, IPROP_BIGICON_ABOUT);
  350.             
  351.             if( x ) 
  352.                 return( MAKELONG(x->m_hWnd,0) ); 
  353.             else 
  354.                 return NULL;
  355.             }            
  356.             break;
  357.             
  358.         case IPROP_BIGICON_SHADOWCOLOR: {
  359.             myPopupWnd *x = new myPopupWnd(this, IPROP_BIGICON_SHADOWCOLOR);
  360.             
  361.             if( x ) 
  362.                 return( MAKELONG( x->m_hWnd, 0) );            
  363.             else    
  364.                 return NULL;
  365.             }   
  366.             break;
  367.     }
  368.  
  369.     //
  370.     // Call the parent method.
  371.     //
  372.     
  373.     return CVBCtlWnd::OnVBInitPropPopup(wp, lp);
  374. }        
  375.  
  376. //
  377. // myCtlWnd::OnCreate
  378. //
  379. // Set the default values for user defined properties.
  380. //
  381. int myCtlWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
  382. {
  383.     if (CVBCtlWnd::OnCreate(lpCreateStruct) == -1)
  384.         return -1;
  385.  
  386.     VBSetControlProperty(m_hCtl, IPROP_BIGICON_BKCOLOR, 
  387.         ::GetSysColor(COLOR_WINDOW));
  388.         
  389.     VBSetControlProperty(m_hCtl, IPROP_BIGICON_SHADOWWIDTH, 4 );
  390.     VBSetControlProperty(m_hCtl, IPROP_BIGICON_SHADOWCOLOR,
  391.         RGB(0,0,0));
  392.                 
  393.     return 0;
  394. }
  395.  
  396. //
  397. // myCtlWnd::OnNcDestroy
  398. //
  399. // Free memory used by picture objects.
  400. //
  401. void myCtlWnd::OnNcDestroy()
  402. {
  403.     CVBCtlWnd::OnNcDestroy();
  404.  
  405.     //
  406.     // Release the picture.
  407.     //
  408.     
  409.     LPBIGICON pict;
  410.     
  411.     pict = (LPBIGICON)LpBigIconDEREF(m_hCtl);
  412.     VBFreePic(pict->hpicPict);    
  413. }
  414.  
  415. //
  416. // myPopupWnd::DoPopupDlg
  417. //
  418. // Virtual function that must be overloaded with your implementation.
  419. //
  420. void myPopupWnd::DoPopupDlg()
  421. {
  422.     ASSERT_VALID(this);
  423.     
  424.     switch( m_iProperty ) {
  425.         case IPROP_BIGICON_ABOUT: {
  426.                 CVBDialog    *AboutDlg = new CVBDialog(IDD_ABOUTDLG, this);
  427.                 AboutDlg->DoModal();    
  428.                 delete AboutDlg;
  429.             }
  430.             break;
  431.                         
  432.         case IPROP_BIGICON_SHADOWCOLOR: {
  433.                 CVBColorDialog     *ShadowColorDlg = new CVBColorDialog(0,0, this );
  434.                 if( ShadowColorDlg->DoModal() == IDOK)
  435.                     SetControlProperty((LONG) ShadowColorDlg->GetColor());
  436.                 delete ShadowColorDlg;
  437.             } 
  438.             break;
  439.     }
  440. }
  441.  
  442.